package com.cencat.common.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;

/**
 * 分页查询参数
 * 用于统一分页查询的请求参数
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class PageQuery extends TenantBaseDTO {
    
    private static final long serialVersionUID = 1L;
    
    /**
     * 当前页码
     */
    @Schema(description = "当前页码")
    @Min(value = 1, message = "页码必须大于0")
    private Integer pageNum = 1;
    
    /**
     * 每页大小
     */
    @Schema(description = "每页大小")
    @Min(value = 1, message = "每页大小必须大于0")
    @Max(value = 1000, message = "每页大小不能超过1000")
    private Integer pageSize = 20;
    
    /**
     * 排序字段
     */
    @Schema(description = "排序字段")
    private String orderBy;
    
    /**
     * 排序方向
     */
    @Schema(description = "排序方向")
    private OrderDirection orderDirection = OrderDirection.DESC;
    
    /**
     * 搜索关键词
     */
    @Schema(description = "搜索关键词")
    private String keyword;
    
    /**
     * 开始时间
     */
    @Schema(description = "开始时间")
    private String startTime;
    
    /**
     * 结束时间
     */
    @Schema(description = "结束时间")
    private String endTime;
    
    /**
     * 状态
     */
    @Schema(description = "状态")
    private Integer status;
    
    /**
     * 获取偏移量
     * @return 偏移量
     */
    public Integer getOffset() {
        return (pageNum - 1) * pageSize;
    }
    
    /**
     * 获取排序SQL
     * @return 排序SQL
     */
    public String getOrderBySql() {
        if (orderBy == null || orderBy.trim().isEmpty()) {
            return "id " + orderDirection.name();
        }
        return orderBy + " " + orderDirection.name();
    }
    
    /**
     * 排序方向枚举
     */
    public enum OrderDirection {
        /**
         * 升序
         */
        ASC,
        
        /**
         * 降序
         */
        DESC
    }
}